cmake_minimum_required(VERSION 3.8)
project(gripper_control)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(Threads REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(MODBUS REQUIRED libmodbus)

if(MODBUS_FOUND)
    message(STATUS "Found libmodbus: ${MODBUS_LIBRARIES}")
else()
    message(FATAL_ERROR "libmodbus not found")
endif()

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/GripperControl.srv"
  "msg/GripperStatus.msg"
)

ament_export_dependencies(rosidl_default_runtime)

include_directories(include)

# 创建共享库
add_library(servo_gripper SHARED src/gripper.cpp)

# 定义 GripperControlNode 库
add_library(gripper_control_node_lib SHARED src/gripper_control_node.cpp)
ament_target_dependencies(gripper_control_node_lib rclcpp)
set(typesupport_target "${PROJECT_NAME}__rosidl_typesupport_cpp")
rosidl_get_typesupport_target(typesupport_target_cpp "${PROJECT_NAME}" "rosidl_typesupport_cpp")
target_link_libraries(gripper_control_node_lib "${typesupport_target_cpp}")

# 定义客户端可执行文件
add_executable(gripper_client src/gripper_client.cpp)
ament_target_dependencies(gripper_client rclcpp)
target_link_libraries(gripper_client 
    gripper_control_node_lib 
    "${typesupport_target_cpp}"
)

# 定义主可执行文件
add_executable(main src/main.cpp)
ament_target_dependencies(main rclcpp)
target_link_libraries(main 
    gripper_control_node_lib 
    "${typesupport_target_cpp}"
    servo_gripper
    Threads::Threads
    ${MODBUS_LIBRARIES}
)

install(TARGETS
  gripper_client
  main
  DESTINATION lib/${PROJECT_NAME}
)

install(TARGETS
  gripper_control_node_lib
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  set(ament_cmake_copyright_FOUND TRUE)
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()